/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 07 - 05
 * Time: 18:08
 */
class Solution6 {
    public boolean isHappy(int n) {//包络1,10,100这些特殊情况
        int slow = n;
        int fast = n;
        while(fast != 1) {
            slow = sum(slow);
            fast = sum(sum(fast));
            if(slow == fast) {
                break;
            }
        }
        return fast == 1;
    }
    private int sum(int n) {
        int sum = 0;
        while(n != 0) {
            sum += (n % 10) * (n % 10);
            n /= 10;
        }
        return sum;
    }
}